package com.longxi.tianjintianbao.greedao.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import com.longxi.tianjintianbao.greedao.entity.Patrol;

import com.longxi.tianjintianbao.greedao.entity.PatrolContent;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "PATROL_CONTENT".
*/
public class PatrolContentDao extends AbstractDao<PatrolContent, Long> {

    public static final String TABLENAME = "PATROL_CONTENT";

    /**
     * Properties of entity PatrolContent.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property Billid = new Property(1, Long.class, "billid", false, "BILLID");
        public final static Property Iptype = new Property(2, Long.class, "iptype", false, "IPTYPE");
        public final static Property Item = new Property(3, String.class, "item", false, "ITEM");
        public final static Property Itemcontent = new Property(4, String.class, "itemcontent", false, "ITEMCONTENT");
    }

    private DaoSession daoSession;

    private Query<PatrolContent> patrol_ContentQuery;

    public PatrolContentDao(DaoConfig config) {
        super(config);
    }
    
    public PatrolContentDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"PATROL_CONTENT\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"BILLID\" INTEGER," + // 1: billid
                "\"IPTYPE\" INTEGER," + // 2: iptype
                "\"ITEM\" TEXT," + // 3: item
                "\"ITEMCONTENT\" TEXT);"); // 4: itemcontent
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"PATROL_CONTENT\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, PatrolContent entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long billid = entity.getBillid();
        if (billid != null) {
            stmt.bindLong(2, billid);
        }
 
        Long iptype = entity.getIptype();
        if (iptype != null) {
            stmt.bindLong(3, iptype);
        }
 
        String item = entity.getItem();
        if (item != null) {
            stmt.bindString(4, item);
        }
 
        String itemcontent = entity.getItemcontent();
        if (itemcontent != null) {
            stmt.bindString(5, itemcontent);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, PatrolContent entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long billid = entity.getBillid();
        if (billid != null) {
            stmt.bindLong(2, billid);
        }
 
        Long iptype = entity.getIptype();
        if (iptype != null) {
            stmt.bindLong(3, iptype);
        }
 
        String item = entity.getItem();
        if (item != null) {
            stmt.bindString(4, item);
        }
 
        String itemcontent = entity.getItemcontent();
        if (itemcontent != null) {
            stmt.bindString(5, itemcontent);
        }
    }

    @Override
    protected final void attachEntity(PatrolContent entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public PatrolContent readEntity(Cursor cursor, int offset) {
        PatrolContent entity = new PatrolContent( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // billid
            cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2), // iptype
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // item
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4) // itemcontent
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, PatrolContent entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setBillid(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
        entity.setIptype(cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2));
        entity.setItem(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setItemcontent(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(PatrolContent entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(PatrolContent entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(PatrolContent entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "content" to-many relationship of Patrol. */
    public List<PatrolContent> _queryPatrol_Content(Long billid) {
        synchronized (this) {
            if (patrol_ContentQuery == null) {
                QueryBuilder<PatrolContent> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.Billid.eq(null));
                patrol_ContentQuery = queryBuilder.build();
            }
        }
        Query<PatrolContent> query = patrol_ContentQuery.forCurrentThread();
        query.setParameter(0, billid);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getPatrolDao().getAllColumns());
            builder.append(" FROM PATROL_CONTENT T");
            builder.append(" LEFT JOIN PATROL T0 ON T.\"BILLID\"=T0.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected PatrolContent loadCurrentDeep(Cursor cursor, boolean lock) {
        PatrolContent entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Patrol patrol = loadCurrentOther(daoSession.getPatrolDao(), cursor, offset);
        entity.setPatrol(patrol);

        return entity;    
    }

    public PatrolContent loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<PatrolContent> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<PatrolContent> list = new ArrayList<PatrolContent>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<PatrolContent> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<PatrolContent> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
